﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace BssDs.Client.UI.Controls.ExtendedControl.Tab
{
	/// <summary>
	/// TabItemEx.xaml の相互作用ロジック
	/// </summary>
	public partial class TabItemEx : TabItem
	{
		public TabItemEx()
		{
			InitializeComponent();
		}

		public enum TabImages
		{
			Selected,
			UnSelected,
			Waiting,
			Modified
		}
	
		
		/// <summary>
		/// ルーティングイベントの登録
		/// </summary>
		public static readonly RoutedEvent TabItemClosingEvent =
			EventManager.RegisterRoutedEvent("TabItemClosing", RoutingStrategy.Bubble,
			typeof(RoutedEventHandler), typeof(TabItemEx));

		// 1. 依存プロパティの作成
		public static readonly DependencyProperty TitleProperty =
			DependencyProperty.Register("Title",
										typeof(string),
										typeof(TabItemEx),
										new FrameworkPropertyMetadata("Title", new PropertyChangedCallback(tabItemTitle_TextChanged)));

		// CloseTabイベント
		/// <summary>
		/// タブの閉じるイベント
		/// </summary>
		public event RoutedEventHandler TabItemClosing
		{
			add
			{
				AddHandler(TabItemClosingEvent, value);
			}
			remove
			{
				RemoveHandler(TabItemClosingEvent, value);
			}
		}

		public string Title
		{
			get { return (string)GetValue(TitleProperty); }
			set { SetValue(TitleProperty, value); }
		}

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			this.SetTabItemImage(this.IsSelected);
			this.SetCloseButtonImage(false);

			this.ContentPanel.MouseEnter += new MouseEventHandler(ContentPanel_MouseEnter);
			this.ContentPanel.MouseLeave += new MouseEventHandler(ContentPanel_MouseLeave);

			this.TabItemCloseButton.Click += new RoutedEventHandler(TabItemCloseButton_Click);
		}

		//protected override void OnMouseEnter(MouseEventArgs e)
		//{
		//    base.OnMouseEnter(e);

		//    this.SetCloseButtonImage(true);
		//}

		//protected override void OnMouseLeave(MouseEventArgs e)
		//{
		//    base.OnMouseLeave(e);

		//    this.SetCloseButtonImage(false);
		//}
		private void ContentPanel_MouseEnter(object sender, MouseEventArgs e)
		{
			this.SetCloseButtonImage(true);
		}

		private void ContentPanel_MouseLeave(object sender, MouseEventArgs e)
		{
			this.SetCloseButtonImage(false);
		}

		private static void tabItemTitle_TextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			// オブジェクトを取得して処理する
			TabItemEx ctrl = obj as TabItemEx;
			if (ctrl != null)
			{
				ctrl.TabItemTitle.Text = ctrl.Title;
			}
		}

		/// <summary>
		/// タブの削除ボタンを押下したときの処理
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void TabItemCloseButton_Click(object sender, RoutedEventArgs e)
		{
			// TabItemClosingイベントを発生させる
			this.RaiseEvent(new RoutedEventArgs(TabItemClosingEvent));
		}


		public void SetCloseButtonImage(bool isSelected)
		{
			Uri uri;

			if (isSelected)
			{
				uri = new Uri("/BuiltInResource;component/Images/1385_Disable_16x16_72.png", UriKind.Relative);
			}
			else
			{
				uri = new Uri("/BuiltInResource;component/Images/305_Close_16x16_72.png", UriKind.Relative);
			}

			this.TabItemCloseImage.Source = new BitmapImage(uri);
		}

		public void SetTabItemImage(bool isSelected)
		{
			TabImages image;

			if (isSelected)
				image = TabImages.Selected;
			else
				image = TabImages.UnSelected;

			this.SetTabItemImageInside(image);
		}
		public void SetTabItemImage(TabImages image)
		{
			this.SetTabItemImageInside(image);
		}
		private void SetTabItemImageInside(TabImages image)
		{
			Uri uri;

			switch (image)
			{
				case TabImages.Selected:
					uri = new Uri("/BuiltInResource;component/Images/Book_openHS.png", UriKind.Relative);
					break;
				case TabImages.UnSelected:
					uri = new Uri("/BuiltInResource;component/Images/Book_angleHS.png", UriKind.Relative);
					break;
				case TabImages.Waiting:
					uri = new Uri("/BuiltInResource;component/Images/browse.png", UriKind.Relative);
					break;
				case TabImages.Modified:
					uri = new Uri("/BuiltInResource;component/Images/327_Options_16x16_72.png", UriKind.Relative);
					break;
				default:
					throw new InvalidOperationException("TabImagesを指定してください。");
			}

			this.TabItemImage.Source = new BitmapImage(uri);
		}
	}
}
